# -*- coding: utf-8 -*-
from datetime import timedelta
from utils.operators.spark_sql_operator import SparkSqlOperator
from spmi.ods.mysql.spmi import spmi_ods__spmi_piece_bill
from spmi.time_sensor.time_after_08_00 import time_after_08_00
from spmi.time_sensor.time_after_06_30 import time_after_06_30

spmi_dwd__dwd_spmi_piece_bill_new_dt_part1= SparkSqlOperator(
    task_id='spmi_dwd__dwd_spmi_piece_bill_new_dt_part1',
    email=['lukunming@jtexpress.com', 'yl_bigdata@yl-scm.com'],
    task_concurrency=1,
    master='yarn',
    name='spmi_dwd__dwd_spmi_piece_bill_new_dt_part1_{{ execution_date | date_add(1) | cst_ds }}',
    sql='spmi/dwd/spmi/dwd_spmi_piece_bill_new_dt/execute_part1.sql',
    pool_slots=3,
    execution_timeout=timedelta(hours=2),
    driver_cores=3,
    driver_memory='9G',
    executor_cores=6,
    executor_memory='9G',
    num_executors=100,
    conf={'spark.dynamicAllocation.enabled': 'true',  # 动态资源开启
          'spark.shuffle.service.enabled': 'true',  # 动态资源 Shuffle 服务开启
          'spark.dynamicAllocation.maxExecutors': 110,  # 动态资源最大扩容 Executor 数
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 60,  # 动态资源自动释放闲置 Executor 的超时时间(s)
          'spark.sql.sources.partitionOverwriteMode': 'dynamic',  # 允许删改已存在的分区
          'spark.executor.memoryOverhead': '6G',  # 堆外内存
          'spark.shuffle.consolidateFiles': 'true',
          'spark.sql.shuffle.partitions': 2000,
          # 'spark.default.parallelism': 1600,
          'spark.shuffle.memoryFraction': '0.5',
          'spark.shuffle.file.buffer':'64k',
          # 'spark.executor.extraJavaOptions': '-XX:+UseG1GC -XX:ParallelGCThreads=6 -XX:ConcGCThreads=2',
          'spark.io.compression.codec': 'org.apache.spark.io.ZStdCompressionCodec'
          },
    hiveconf={'hive.exec.dynamic.partition': 'true',  # 动态分区
              'hive.exec.dynamic.partition.mode': 'nonstrict',
              'hive.exec.max.dynamic.partitions': 200,  # 每天生成 20 个分区
              'hive.exec.max.dynamic.partitions.pernode': 200,  # 每天生成 20 个分区
              },
    yarn_queue='pro',
)

spmi_dwd__dwd_spmi_piece_bill_new_dt_part1 << [spmi_ods__spmi_piece_bill,time_after_06_30]


spmi_dwd__dwd_spmi_piece_bill_new_dt_part2= SparkSqlOperator(
    task_id='spmi_dwd__dwd_spmi_piece_bill_new_dt_part2',
    email=['lukunming@jtexpress.com', 'yl_bigdata@yl-scm.com'],
    task_concurrency=1,
    master='yarn',
    name='spmi_dwd__dwd_spmi_piece_bill_new_dt_part2_{{ execution_date | date_add(1) | cst_ds }}',
    sql='spmi/dwd/spmi/dwd_spmi_piece_bill_new_dt/execute_part2.sql',
    pool_slots=3,
    execution_timeout=timedelta(hours=2),
    driver_cores=4,
    driver_memory='8G',
    executor_cores=6,
    executor_memory='12G',
    num_executors=80,
    conf={'spark.dynamicAllocation.enabled': 'true',  # 动态资源开启
          'spark.shuffle.service.enabled': 'true',  # 动态资源 Shuffle 服务开启
          'spark.dynamicAllocation.maxExecutors': 100,  # 动态资源最大扩容 Executor 数
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 60,  # 动态资源自动释放闲置 Executor 的超时时间(s)
          'spark.sql.sources.partitionOverwriteMode': 'dynamic',  # 允许删改已存在的分区
          'spark.executor.memoryOverhead': '6G',  # 堆外内存
          'spark.sql.shuffle.partitions': 2000,
          'spark.default.parallelism': 1600,
          'spark.shuffle.consolidateFiles': 'true',
          'spark.shuffle.memoryFraction': '0.5',
          # 'spark.shuffle.file.buffer':'64k',
          # 'spark.executor.extraJavaOptions': '-XX:+UseG1GC -XX:ParallelGCThreads=6 -XX:ConcGCThreads=2',
          'spark.io.compression.codec': 'org.apache.spark.io.ZStdCompressionCodec'
          },
    hiveconf={'hive.exec.dynamic.partition': 'true',  # 动态分区
              'hive.exec.dynamic.partition.mode': 'nonstrict',
              'hive.exec.max.dynamic.partitions': 200,  # 每天生成 20 个分区
              'hive.exec.max.dynamic.partitions.pernode': 200,  # 每天生成 20 个分区
              },
    yarn_queue='pro'
)

spmi_dwd__dwd_spmi_piece_bill_new_dt_part2 << [spmi_ods__spmi_piece_bill,time_after_08_00]